]> sigrok.org Git - libsigrok.git/blob - src/input/trace32_ad.c
6cb6bcb4d51f2d6a2fc97a7b6d51828db0bc0947
[libsigrok.git] / src / input / trace32_ad.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Soeren Apel <soeren@apelpie.net>
5  * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Usage notes:
23  * This input module reads .ad files created using the
24  * following practice commands:
25  *
26  * I.SAVE <file> /NoCompress
27  * IPROBE.SAVE <file> /NoCompress
28  *
29  * It currently cannot make use of files that have been
30  * saved using /QuickCompress, /Compress or /ZIP.
31  * As a workaround you may load the file in PowerView
32  * using I.LOAD / IPROBE.LOAD and re-save using /NoCompress.
33  */
34
35 #include <config.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <sys/time.h>
43 #include <libsigrok/libsigrok.h>
44 #include "libsigrok-internal.h"
45
46 #define LOG_PREFIX "input/trace32_ad"
47
48 #define CHUNK_SIZE        (4 * 1024 * 1024)
49 #define MAX_POD_COUNT     12
50 #define HEADER_SIZE       80
51
52 #define TIMESTAMP_RESOLUTION ((double)0.000000000078125) /* 0.078125 ns */
53
54 /*
55  * The resolution equals a sampling freq of 12.8 GHz. That's a bit high
56  * for inter-record sample generation, so we scale it down to 200 MHz
57  * for now. That way, the scaling factor becomes 32.
58  */
59 #define DEFAULT_SAMPLERATE 200
60
61 enum {
62         AD_FORMAT_BINHDR = 1, /* Binary header, binary data, textual setup info */
63         AD_FORMAT_TXTHDR      /* Textual header, binary data */
64 };
65
66 enum {
67         AD_DEVICE_PI = 1, /* Data recorded by LA-7940 PowerIntegrator or */
68                           /* LA-394x PowerIntegrator II. */
69         AD_DEVICE_IPROBE  /* Data recorded by LA-769x PowerTrace II IProbe. */
70         /* Missing file format info for LA-793x ICD PowerProbe */
71         /* Missing file format info for LA-4530 uTrace analog probe */
72 };
73
74 enum {
75         AD_MODE_250MHZ = 0,
76         AD_MODE_500MHZ = 1
77 };
78
79 enum {
80         AD_COMPR_NONE  = 0, /* File created with /NOCOMPRESS */
81         AD_COMPR_QCOMP = 6, /* File created with /COMPRESS or /QUICKCOMPRESS */
82 };
83
84 struct context {
85         gboolean meta_sent;
86         gboolean header_read, records_read, trigger_sent;
87         char format, device, record_mode, compression;
88         char pod_status[MAX_POD_COUNT];
89         struct sr_channel *channels[MAX_POD_COUNT][17]; /* 16 + CLK */
90         uint64_t trigger_timestamp;
91         uint32_t record_size, record_count, cur_record;
92         int32_t last_record;
93         uint64_t samplerate;
94         double timestamp_scale;
95         GString *out_buf;
96 };
97
98 static int process_header(GString *buf, struct context *inc);
99 static void create_channels(struct sr_input *in);
100
101 static char get_pod_name_from_id(int id)
102 {
103         switch (id) {
104         case 0:  return 'A';
105         case 1:  return 'B';
106         case 2:  return 'C';
107         case 3:  return 'D';
108         case 4:  return 'E';
109         case 5:  return 'F';
110         case 6:  return 'J';
111         case 7:  return 'K';
112         case 8:  return 'L';
113         case 9:  return 'M';
114         case 10: return 'N';
115         case 11: return 'O';
116         default:
117                 sr_err("get_pod_name_from_id() called with invalid ID %d!", id);
118         }
119         return 'X';
120 }
121
122 static int init(struct sr_input *in, GHashTable *options)
123 {
124         struct context *inc;
125         int pod;
126         char id[17];
127
128         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
129         in->priv = g_malloc0(sizeof(struct context));
130
131         inc = in->priv;
132
133         /* Calculate the desired timestamp scaling factor. */
134         inc->samplerate = 1000000 *
135                 g_variant_get_uint32(g_hash_table_lookup(options, "samplerate"));
136
137         inc->timestamp_scale = ((1 / TIMESTAMP_RESOLUTION) / (double)inc->samplerate);
138
139         /* Enable the pods the user chose to see. */
140         for (pod = 0; pod < MAX_POD_COUNT; pod++) {
141                 g_snprintf(id, sizeof(id), "pod%c", get_pod_name_from_id(pod));
142                 if (g_variant_get_boolean(g_hash_table_lookup(options, id)))
143                         inc->pod_status[pod] = 1;
144         }
145
146         create_channels(in);
147         if (g_slist_length(in->sdi->channels) == 0) {
148                 sr_err("No pods were selected and thus no channels created, aborting.");
149                 g_free(in->priv);
150                 g_free(in->sdi);
151                 return SR_ERR;
152         }
153
154         inc->out_buf = g_string_sized_new(CHUNK_SIZE);
155
156         return SR_OK;
157 }
158
159 static int format_match(GHashTable *metadata)
160 {
161         GString *buf;
162
163         buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
164
165         return process_header(buf, NULL);
166 }
167
168 static int process_header(GString *buf, struct context *inc)
169 {
170         char *format_name, *format_name_sig;
171         int i, record_size, device_id;
172
173         /*
174          * 00-31 (0x00-1F) file format name
175          * 32-39 (0x20-27) trigger timestamp       u64
176          * 40-47 (0x28-2F) unused
177          * 48    (0x30)    compression
178          * 49-53 (0x31-35) ??
179          *       50 (0x32) 0x00 (PI), 0x01 (iprobe)
180          * 54    (0x36)    0x08 (PI 250/500), 0x0A (iprobe 250)
181          * 55    (0x37)    0x00 (250), 0x01 (500)
182          * 56    (0x38)    record size             u8
183          * 57-59 (0x39-3B) const 0x00
184          * 60-63 (0x3C-3F) number of records       u32
185          * 64-67 (0x40-43) id of last record       s32
186          * 68-77 (0x44-4D) ??
187          *       71 (0x47) const 0x80=128
188          *       72 (0x48) const 0x01
189          * 78-79 (0x4E-4F) ??
190          */
191
192         /* Note: inc is off-limits until we check whether it's a valid pointer. */
193
194         format_name = g_strndup(buf->str, 32);
195
196         /* File format name ends on 0x20/0x1A, let's remove both. */
197         for (i = 1; i < 31; i++) {
198                 if (format_name[i] == 0x1A) {
199                         format_name[i - 1] = 0;
200                         format_name[i] = 0;
201                 }
202         }
203         g_strchomp(format_name); /* This is for additional padding spaces. */
204
205         format_name_sig = g_strndup(format_name, 5);
206
207         /* Desired file formats either start with digit+space or "trace32". */
208         if (g_strcmp0(format_name_sig, "trace32")) {
209                 if (inc)
210                         inc->format = AD_FORMAT_BINHDR;
211         } else if (g_ascii_isdigit(format_name[0]) && (format_name[1] == 0x20)) {
212                 if (inc)
213                         inc->format = AD_FORMAT_TXTHDR;
214                 g_free(format_name_sig);
215                 g_free(format_name);
216                 sr_err("This format isn't implemented yet, aborting.");
217                 return SR_ERR;
218         } else {
219                 g_free(format_name_sig);
220                 g_free(format_name);
221                 sr_err("Don't know this file format, aborting.");
222                 return SR_ERR;
223         }
224
225         sr_dbg("File says it's \"%s\"", format_name);
226
227         record_size = R8(buf->str + 56);
228         device_id = 0;
229
230         if (g_strcmp0(format_name, "trace32 power integrator data") == 0) {
231                 if (record_size == 28 || record_size == 45)
232                         device_id = AD_DEVICE_PI;
233         } else if (g_strcmp0(format_name, "trace32 iprobe data") == 0) {
234                 if (record_size == 11)
235                         device_id = AD_DEVICE_IPROBE;
236         }
237
238         if (!device_id) {
239                 g_free(format_name_sig);
240                 g_free(format_name);
241                 sr_err("Don't know how to handle this file with record size %d.",
242                         record_size);
243                 return SR_ERR;
244         }
245
246         g_free(format_name_sig);
247         g_free(format_name);
248
249         /* Stop processing the header if we just want to identify the file. */
250         if (!inc)
251                 return SR_OK;
252
253         inc->device       = device_id;
254         inc->trigger_timestamp = RL64(buf->str + 32);
255         inc->compression  = R8(buf->str + 48); /* Maps to the enum. */
256         inc->record_mode  = R8(buf->str + 55); /* Maps to the enum. */
257         inc->record_size  = record_size;
258         inc->record_count = RL32(buf->str + 60);
259         inc->last_record  = RL32S(buf->str + 64);
260
261         sr_dbg("Trigger occured at %lf s.",
262                 inc->trigger_timestamp * TIMESTAMP_RESOLUTION);
263         sr_dbg("File contains %d records: first one is %d, last one is %d.",
264                 inc->record_count, (inc->last_record - inc->record_count + 1),
265                 inc->last_record);
266
267         /* Check if we can work with this compression. */
268         if (inc->compression != AD_COMPR_NONE) {
269                 sr_err("File uses unsupported compression (0x%02X), can't continue.",
270                         inc->compression);
271                 return SR_ERR;
272         }
273
274         inc->header_read = TRUE;
275
276         return SR_OK;
277 }
278
279 static void create_channels(struct sr_input *in)
280 {
281         struct context *inc;
282         int pod, channel, chan_id;
283         char name[8];
284
285         inc = in->priv;
286         chan_id = 0;
287
288         for (pod = 0; pod < MAX_POD_COUNT; pod++) {
289                 if (!inc->pod_status[pod])
290                         continue;
291
292                 for (channel = 0; channel < 16; channel++) {
293                         snprintf(name, sizeof(name), "%c%d", get_pod_name_from_id(pod), channel);
294                         inc->channels[pod][channel] =
295                                 sr_channel_new(in->sdi, chan_id, SR_CHANNEL_LOGIC, TRUE, name);
296                         chan_id++;
297                 }
298
299                 snprintf(name, sizeof(name), "CLK%c", get_pod_name_from_id(pod));
300                 inc->channels[pod][16] =
301                         sr_channel_new(in->sdi, chan_id, SR_CHANNEL_LOGIC, TRUE, name);
302                 chan_id++;
303         }
304 }
305
306 static void send_metadata(struct sr_input *in)
307 {
308         struct sr_datafeed_packet packet;
309         struct sr_datafeed_meta meta;
310         struct sr_config *src;
311         struct context *inc;
312
313         inc = in->priv;
314
315         packet.type = SR_DF_META;
316         packet.payload = &meta;
317         src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
318         meta.config = g_slist_append(NULL, src);
319         sr_session_send(in->sdi, &packet);
320         g_slist_free(meta.config);
321         sr_config_free(src);
322
323         inc->meta_sent = TRUE;
324 }
325
326 static void flush_output_buffer(struct sr_input *in)
327 {
328         struct context *inc;
329         struct sr_datafeed_packet packet;
330         struct sr_datafeed_logic logic;
331
332         inc = in->priv;
333
334         if (inc->out_buf->len) {
335                 packet.type = SR_DF_LOGIC;
336                 packet.payload = &logic;
337                 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
338                 logic.data = inc->out_buf->str;
339                 logic.length = inc->out_buf->len;
340                 sr_session_send(in->sdi, &packet);
341
342                 g_string_truncate(inc->out_buf, 0);
343         }
344 }
345
346 static void process_record_pi(struct sr_input *in, gsize start)
347 {
348         struct sr_datafeed_packet packet;
349         struct context *inc;
350         uint64_t timestamp, next_timestamp;
351         uint32_t pod_data;
352         char single_payload[12 * 3];
353         GString *buf;
354         int i, pod_count, clk_offset, packet_count, pod;
355         int payload_bit, payload_len, value;
356
357         inc = in->priv;
358         buf = in->buf;
359
360         /*
361          * 00-07 timestamp
362          * 08-09 A15..0
363          * 10-11 B15..0
364          * 12-13 C15..0
365          * 14-15 D15..0
366          * 16-17 E15..0
367          * 18-19 F15..0
368          * 20-23 ??
369          * 24-25 J15..0                         Not present in 500MHz mode
370          * 26-27 K15..0                         Not present in 500MHz mode
371          * 28-29 L15..0                         Not present in 500MHz mode
372          * 30-31 M15..0                         Not present in 500MHz mode
373          * 32-33 N15..0                         Not present in 500MHz mode
374          * 34-35 O15..0                         Not present in 500MHz mode
375          * 36-39 ??                             Not present in 500MHz mode
376          * 40/24 CLKF..A (32=CLKF, .., 1=CLKA)
377          * 41    CLKO..J (32=CLKO, .., 1=CLKJ)  Not present in 500MHz mode
378          * 42/25    ??
379          * 43/26    ??
380          * 44/27    ??
381          */
382
383         timestamp = RL64(buf->str + start);
384
385         if (inc->record_mode == AD_MODE_500MHZ) {
386                 pod_count = 6;
387                 clk_offset = 24;
388         } else {
389                 pod_count = 12;
390                 clk_offset = 40;
391         }
392
393         payload_bit = 0;
394         payload_len = 0;
395         single_payload[0] = 0;
396
397         for (pod = 0; pod < pod_count; pod++) {
398                 if (!inc->pod_status[pod])
399                         continue;
400
401                 switch (pod) {
402                 case 0: /* A */
403                         pod_data = RL16(buf->str + start + 8);
404                         pod_data |= (RL16(buf->str + start + clk_offset) & 1) << 16;
405                         break;
406                 case 1: /* B */
407                         pod_data = RL16(buf->str + start + 10);
408                         pod_data |= (RL16(buf->str + start + clk_offset) & 2) << 15;
409                         break;
410                 case 2: /* C */
411                         pod_data = RL16(buf->str + start + 12);
412                         pod_data |= (RL16(buf->str + start + clk_offset) & 4) << 14;
413                         break;
414                 case 3: /* D */
415                         pod_data = RL16(buf->str + start + 14);
416                         pod_data |= (RL16(buf->str + start + clk_offset) & 8) << 13;
417                         break;
418                 case 4: /* E */
419                         pod_data = RL16(buf->str + start + 16);
420                         pod_data |= (RL16(buf->str + start + clk_offset) & 16) << 12;
421                         break;
422                 case 5: /* F */
423                         pod_data = RL16(buf->str + start + 18);
424                         pod_data |= (RL16(buf->str + start + clk_offset) & 32) << 11;
425                         break;
426                 case 6: /* J */
427                         pod_data = RL16(buf->str + start + 24);
428                         pod_data |= (RL16(buf->str + start + 41) & 1) << 16;
429                         break;
430                 case 7: /* K */
431                         pod_data = RL16(buf->str + start + 26);
432                         pod_data |= (RL16(buf->str + start + 41) & 2) << 15;
433                         break;
434                 case 8: /* L */
435                         pod_data = RL16(buf->str + start + 28);
436                         pod_data |= (RL16(buf->str + start + 41) & 4) << 14;
437                         break;
438                 case 9: /* M */
439                         pod_data = RL16(buf->str + start + 30);
440                         pod_data |= (RL16(buf->str + start + 41) & 8) << 13;
441                         break;
442                 case 10: /* N */
443                         pod_data = RL16(buf->str + start + 32);
444                         pod_data |= (RL16(buf->str + start + 41) & 16) << 12;
445                         break;
446                 case 11: /* O */
447                         pod_data = RL16(buf->str + start + 34);
448                         pod_data |= (RL16(buf->str + start + 41) & 32) << 11;
449                         break;
450                 default:
451                         sr_err("Don't know how to obtain data for pod %d.", pod);
452                 }
453
454                 for (i = 0; i < 17; i++) {
455                         value = (pod_data >> i) & 1;
456                         single_payload[payload_len] |= value << payload_bit;
457
458                         payload_bit++;
459                         if (payload_bit > 7) {
460                                 payload_bit = 0;
461                                 payload_len++;
462                                 single_payload[payload_len] = 0;
463                         }
464                 }
465         }
466
467         /* Make sure that payload_len accounts for any incomplete bytes used. */
468         if (payload_bit)
469                 payload_len++;
470
471         i = (g_slist_length(in->sdi->channels) + 7) / 8;
472         if (payload_len != i) {
473                 sr_err("Payload unit size is %d but should be %d!", payload_len, i);
474                 return;
475         }
476
477         if (timestamp == inc->trigger_timestamp && !inc->trigger_sent) {
478                 sr_dbg("Trigger @%lf s, record #%d.",
479                         timestamp * TIMESTAMP_RESOLUTION, inc->cur_record);
480
481                 packet.type = SR_DF_TRIGGER;
482                 packet.payload = NULL;
483                 sr_session_send(in->sdi, &packet);
484                 inc->trigger_sent = TRUE;
485         }
486
487         /* Is this the last record in the file? */
488         if (inc->cur_record == inc->record_count - 1) {
489                 /* It is, so send the last sample data only once. */
490                 g_string_append_len(inc->out_buf, single_payload, payload_len);
491         } else {
492                 /* It's not, so fill the time gap by sending lots of data. */
493                 next_timestamp = RL64(buf->str + start + inc->record_size);
494                 packet_count = (int)(next_timestamp - timestamp) / inc->timestamp_scale;
495
496                 /* Make sure we send at least one data set. */
497                 if (packet_count == 0)
498                         packet_count = 1;
499
500                 for (i = 0; i < packet_count; i++)
501                         g_string_append_len(inc->out_buf, single_payload, payload_len);
502         }
503
504         if (inc->out_buf->len >= CHUNK_SIZE)
505                 flush_output_buffer(in);
506 }
507
508 static void process_record_iprobe(struct sr_input *in, gsize start)
509 {
510         struct sr_datafeed_packet packet;
511         struct context *inc;
512         uint64_t timestamp, next_timestamp;
513         char single_payload[3];
514         int i, payload_len, packet_count;
515
516         inc = in->priv;
517
518         /*
519          * 00-07 timestamp
520          * 08-09 IP15..0
521          * 10    CLK
522          */
523
524         timestamp = RL64(in->buf->str + start);
525         single_payload[0] = R8(in->buf->str + start + 8);
526         single_payload[1] = R8(in->buf->str + start + 9);
527         single_payload[2] = R8(in->buf->str + start + 10) & 1;
528         payload_len = 3;
529
530         if (timestamp == inc->trigger_timestamp && !inc->trigger_sent) {
531                 sr_dbg("Trigger @%lf s, record #%d.",
532                         timestamp * TIMESTAMP_RESOLUTION, inc->cur_record);
533
534                 packet.type = SR_DF_TRIGGER;
535                 packet.payload = NULL;
536                 sr_session_send(in->sdi, &packet);
537                 inc->trigger_sent = TRUE;
538         }
539
540         /* Is this the last record in the file? */
541         if (inc->cur_record == inc->record_count - 1) {
542                 /* It is, so send the last sample data only once. */
543                 g_string_append_len(inc->out_buf, single_payload, payload_len);
544         } else {
545                 /* It's not, so fill the time gap by sending lots of data. */
546                 next_timestamp = RL64(in->buf->str + start + inc->record_size);
547                 packet_count = (int)(next_timestamp - timestamp) / inc->timestamp_scale;
548
549                 /* Make sure we send at least one data set. */
550                 if (packet_count == 0)
551                         packet_count = 1;
552
553                 for (i = 0; i < packet_count; i++)
554                         g_string_append_len(inc->out_buf, single_payload, payload_len);
555         }
556
557         if (inc->out_buf->len >= CHUNK_SIZE)
558                 flush_output_buffer(in);
559 }
560
561 static void process_practice_token(struct sr_input *in, char *cmd_token)
562 {
563         struct context *inc;
564         char **tokens;
565         char chan_suffix[2], chan_name[33];
566         char *s1, *s2;
567         int pod, ch;
568         struct sr_channel *channel;
569
570         inc = in->priv;
571
572         /*
573          * Commands of interest (I may also be IPROBE):
574          *
575          * I.TWIDTH
576          * I.TPREDELAY
577          * I.TDELAY
578          * I.TYSNC.SELECT I.A0 HIGH
579          * NAME.SET <port.chan> <name> <+/-> ...
580          */
581
582         if (!cmd_token)
583                 return;
584
585         if (cmd_token[0] == 0)
586                 return;
587
588         tokens = g_strsplit(cmd_token, " ", 0);
589
590         if (!tokens)
591                 return;
592
593         if (g_strcmp0(tokens[0], "NAME.SET") == 0) {
594                 /* Let the user know when the channel has been inverted. */
595                 /* This *should* be token #3 but there's an additonal space, making it #4. */
596                 chan_suffix[0] = 0;
597                 chan_suffix[1] = 0;
598                 if (tokens[4]) {
599                         if (tokens[4][0] == '-')
600                                 chan_suffix[0] = '-'; /* This is the way PowerView shows it. */
601                 }
602
603                 /*
604                  * Command is using structure "NAME.SET I.A00 I.XYZ" or
605                  * "NAME.SET IP.00 IP.XYZ", depending on the device used.
606                  * Let's get strings with the I./IP. from both tokens removed.
607                  */
608                 s1 = g_strstr_len(tokens[1], -1, ".") + 1;
609                 s2 = g_strstr_len(tokens[2], -1, ".") + 1;
610
611                 if (g_strcmp0(s1, "CLK") == 0) {
612                         /* CLK for iprobe */
613                         pod = 0;
614                         ch = 16;
615                 } else if ((strlen(s1) == 4) && g_ascii_isupper(s1[3])) {
616                         /* CLKA/B/J/K for PowerIntegrator */
617                         pod = s1[3] - (char)'A';
618                         ch = 16;
619                 } else if (g_ascii_isupper(s1[0])) {
620                         /* A00 for PowerIntegrator */
621                         pod = s1[0] - (char)'A';
622                         ch = atoi(s1 + 1);
623                 } else {
624                         /* 00 for iprobe */
625                         pod = 0;
626                         ch = atoi(s1);
627                 }
628
629                 channel = inc->channels[pod][ch];
630                 g_snprintf(chan_name, sizeof(chan_name), "%s%s", s2, chan_suffix);
631
632                 sr_dbg("Changing channel name for %s to %s.", s1, chan_name);
633                 sr_dev_channel_name_set(channel, chan_name);
634         }
635
636         g_strfreev(tokens);
637 }
638
639 static void process_practice(struct sr_input *in)
640 {
641         char delimiter[3];
642         char **tokens, *token;
643         int i;
644
645         /* Gather all input data until we see the end marker. */
646         if (in->buf->str[in->buf->len - 1] != 0x29)
647                 return;
648
649         delimiter[0] = 0x0A;
650         delimiter[1] = ' ';
651         delimiter[2] = 0;
652
653         tokens = g_strsplit(in->buf->str, delimiter, 0);
654
655         /* Special case: first token contains the start marker, too. Skip it. */
656         token = tokens[0];
657         for (i = 0; token[i]; i++) {
658                 if (token[i] == ' ')
659                         process_practice_token(in, token + i + 1);
660         }
661
662         for (i = 1; tokens[i]; i++)
663                 process_practice_token(in, tokens[i]);
664
665         g_strfreev(tokens);
666
667         g_string_erase(in->buf, 0, in->buf->len);
668 }
669
670 static int process_buffer(struct sr_input *in)
671 {
672         struct context *inc;
673         int i, chunk_size, res;
674
675         inc = in->priv;
676
677         if (!inc->header_read) {
678                 res = process_header(in->buf, inc);
679                 g_string_erase(in->buf, 0, HEADER_SIZE);
680                 if (res != SR_OK)
681                         return res;
682         }
683
684         if (!inc->meta_sent) {
685                 std_session_send_df_header(in->sdi);
686                 send_metadata(in);
687         }
688
689         if (!inc->records_read) {
690                 /* Cut off at a multiple of the record size. */
691                 chunk_size = ((in->buf->len) / inc->record_size) * inc->record_size;
692
693                 /* There needs to be at least one more record process_record() can peek into. */
694                 chunk_size -= inc->record_size;
695
696                 for (i = 0; (i < chunk_size) && (!inc->records_read); i += inc->record_size) {
697                         switch (inc->device) {
698                         case AD_DEVICE_PI:
699                                 process_record_pi(in, i);
700                                 break;
701                         case AD_DEVICE_IPROBE:
702                                 process_record_iprobe(in, i);
703                                 break;
704                         default:
705                                 sr_err("Trying to process records for unknown device!");
706                                 return SR_ERR;
707                         }
708
709                         inc->cur_record++;
710                         if (inc->cur_record == inc->record_count)
711                                 inc->records_read = TRUE;
712                 }
713
714                 g_string_erase(in->buf, 0, i);
715         }
716
717         if (inc->records_read) {
718                 /* Read practice commands that configure the setup. */
719                 process_practice(in);
720         }
721
722         return SR_OK;
723 }
724
725 static int receive(struct sr_input *in, GString *buf)
726 {
727         g_string_append_len(in->buf, buf->str, buf->len);
728
729         if (!in->sdi_ready) {
730                 /* sdi is ready, notify frontend. */
731                 in->sdi_ready = TRUE;
732                 return SR_OK;
733         }
734
735         return process_buffer(in);
736 }
737
738 static int end(struct sr_input *in)
739 {
740         struct context *inc;
741         int ret;
742
743         inc = in->priv;
744
745         if (in->sdi_ready)
746                 ret = process_buffer(in);
747         else
748                 ret = SR_OK;
749
750         flush_output_buffer(in);
751
752         if (inc->meta_sent)
753                 std_session_send_df_end(in->sdi);
754
755         return ret;
756 }
757
758 static int reset(struct sr_input *in)
759 {
760         struct context *inc = in->priv;
761
762         inc->meta_sent = FALSE;
763         inc->header_read = FALSE;
764         inc->records_read = FALSE;
765         inc->trigger_sent = FALSE;
766         inc->cur_record = 0;
767
768         g_string_truncate(in->buf, 0);
769
770         return SR_OK;
771 }
772
773 static struct sr_option options[] = {
774         { "podA", "Import pod A / iprobe",
775                 "Create channels and data for pod A / iprobe", NULL, NULL },
776
777         { "podB", "Import pod B", "Create channels and data for pod B", NULL, NULL },
778         { "podC", "Import pod C", "Create channels and data for pod C", NULL, NULL },
779         { "podD", "Import pod D", "Create channels and data for pod D", NULL, NULL },
780         { "podE", "Import pod E", "Create channels and data for pod E", NULL, NULL },
781         { "podF", "Import pod F", "Create channels and data for pod F", NULL, NULL },
782         { "podJ", "Import pod J", "Create channels and data for pod J", NULL, NULL },
783         { "podK", "Import pod K", "Create channels and data for pod K", NULL, NULL },
784         { "podL", "Import pod L", "Create channels and data for pod L", NULL, NULL },
785         { "podM", "Import pod M", "Create channels and data for pod M", NULL, NULL },
786         { "podN", "Import pod N", "Create channels and data for pod N", NULL, NULL },
787         { "podO", "Import pod O", "Create channels and data for pod O", NULL, NULL },
788
789         { "samplerate", "Reduced sample rate (MHz)", "Reduce the original sample rate of 12.8 GHz to the specified sample rate in MHz", NULL, NULL },
790
791         ALL_ZERO
792 };
793
794 static const struct sr_option *get_options(void)
795 {
796         if (!options[0].def) {
797                 options[0].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
798                 options[1].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
799                 options[2].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
800                 options[3].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
801                 options[4].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
802                 options[5].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
803                 options[6].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
804                 options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
805                 options[8].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
806                 options[9].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
807                 options[10].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
808                 options[11].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
809                 options[12].def = g_variant_ref_sink(g_variant_new_uint32(DEFAULT_SAMPLERATE));
810         }
811
812         return options;
813 }
814
815 SR_PRIV struct sr_input_module input_trace32_ad = {
816         .id = "trace32_ad",
817         .name = "Trace32_ad",
818         .desc = "Lauterbach Trace32 logic analyzer data",
819         .exts = (const char*[]){"ad", NULL},
820         .options = get_options,
821         .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
822         .format_match = format_match,
823         .init = init,
824         .receive = receive,
825         .end = end,
826         .reset = reset,
827 };