]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
input/chronovu_la8: address file size data type nits
[libsigrok.git] / src / input / chronovu_la8.c
CommitLineData
83e9d586 1/*
50985c20 2 * This file is part of the libsigrok project.
83e9d586
UH
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
83e9d586
UH
18 */
19
6ec6c43b 20#include <config.h>
83e9d586
UH
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
36423d04 24#include <sys/stat.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
83e9d586 27
b95dd761 28#define LOG_PREFIX "input/chronovu-la8"
8e7f1cfd 29
02e24c0c 30#define DEFAULT_NUM_CHANNELS 8
1a46cc62 31#define DEFAULT_SAMPLERATE SR_MHZ(100)
9a4fd01a 32#define CHUNK_SIZE (4 * 1024 * 1024)
1a46cc62 33#define CHRONOVU_LA8_FILESIZE ((8 * 1024 * 1024) + 5)
83e9d586 34
02e24c0c
BV
35struct context {
36 gboolean started;
37 uint64_t samplerate;
38};
83e9d586 39
54ee427d 40static int format_match(GHashTable *metadata, unsigned int *confidence)
83e9d586 41{
f54a55da 42 uint64_t size;
83e9d586 43
54ee427d
GS
44 /*
45 * In the absence of a reliable condition like magic strings,
46 * we can only guess based on the file size. Since this is
47 * rather weak a condition, signal "little confidence" and
48 * optionally give precedence to better matches.
49 */
f54a55da 50 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
02e24c0c 51 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
54ee427d
GS
52 if (size != CHRONOVU_LA8_FILESIZE)
53 return SR_ERR;
54 *confidence = 100;
83e9d586 55
54ee427d 56 return SR_OK;
83e9d586
UH
57}
58
02e24c0c 59static int init(struct sr_input *in, GHashTable *options)
83e9d586 60{
02e24c0c 61 struct context *inc;
ba7dd8bb 62 int num_channels, i;
02e24c0c
BV
63 char name[16];
64
65 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
66 if (num_channels < 1) {
67 sr_err("Invalid value for numchannels: must be at least 1.");
68 return SR_ERR_ARG;
69 }
70
aac29cc1 71 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
02e24c0c 72 in->priv = inc = g_malloc0(sizeof(struct context));
edd28877
BV
73
74 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 75
ba7dd8bb 76 for (i = 0; i < num_channels; i++) {
00ed77f2 77 snprintf(name, sizeof(name), "%d", i);
5e23fcab 78 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
464d12c7 79 }
83e9d586
UH
80
81 return SR_OK;
82}
83
7066fd46 84static int process_buffer(struct sr_input *in)
83e9d586 85{
83e9d586 86 struct sr_datafeed_packet packet;
2df1e819 87 struct sr_datafeed_meta meta;
9c939c51 88 struct sr_datafeed_logic logic;
2df1e819 89 struct sr_config *src;
02e24c0c
BV
90 struct context *inc;
91 gsize chunk_size, i;
7066fd46 92 int chunk;
83e9d586 93
02e24c0c 94 inc = in->priv;
04c2f202 95 if (!inc->started) {
bee2b016 96 std_session_send_df_header(in->sdi);
04c2f202 97
d0181813
BV
98 if (inc->samplerate) {
99 packet.type = SR_DF_META;
100 packet.payload = &meta;
101 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
102 meta.config = g_slist_append(NULL, src);
103 sr_session_send(in->sdi, &packet);
c01378c9 104 g_slist_free(meta.config);
d0181813
BV
105 sr_config_free(src);
106 }
107
108 inc->started = TRUE;
04c2f202 109 }
83e9d586 110
83e9d586 111 packet.type = SR_DF_LOGIC;
9c939c51 112 packet.payload = &logic;
7066fd46 113 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
02e24c0c
BV
114
115 /* Cut off at multiple of unitsize. */
116 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
83e9d586 117
02e24c0c 118 for (i = 0; i < chunk_size; i += chunk) {
04c2f202 119 logic.data = in->buf->str + i;
8bc2fa6d 120 chunk = MIN(CHUNK_SIZE, chunk_size - i);
02e24c0c 121 logic.length = chunk;
5c3c1241 122 sr_session_send(in->sdi, &packet);
83e9d586 123 }
04c2f202 124 g_string_erase(in->buf, 0, chunk_size);
02e24c0c
BV
125
126 return SR_OK;
127}
128
7066fd46
BV
129static int receive(struct sr_input *in, GString *buf)
130{
131 int ret;
132
133 g_string_append_len(in->buf, buf->str, buf->len);
134
135 if (!in->sdi_ready) {
136 /* sdi is ready, notify frontend. */
137 in->sdi_ready = TRUE;
138 return SR_OK;
139 }
140
141 ret = process_buffer(in);
142
143 return ret;
144}
145
146static int end(struct sr_input *in)
02e24c0c 147{
02e24c0c 148 struct context *inc;
7066fd46 149 int ret;
02e24c0c 150
7066fd46
BV
151 if (in->sdi_ready)
152 ret = process_buffer(in);
153 else
154 ret = SR_OK;
02e24c0c 155
7066fd46 156 inc = in->priv;
3be42bc2 157 if (inc->started)
bee2b016 158 std_session_send_df_end(in->sdi);
83e9d586 159
7066fd46 160 return ret;
83e9d586
UH
161}
162
ef9d3fef
SA
163static int reset(struct sr_input *in)
164{
165 struct context *inc = in->priv;
166
167 inc->started = FALSE;
168 g_string_truncate(in->buf, 0);
169
170 return SR_OK;
171}
172
02e24c0c 173static struct sr_option options[] = {
867293a1
UH
174 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
175 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
06ad20be 176 ALL_ZERO
02e24c0c
BV
177};
178
2c240774 179static const struct sr_option *get_options(void)
02e24c0c
BV
180{
181 if (!options[0].def) {
182 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
edd28877 183 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
02e24c0c
BV
184 }
185
186 return options;
187}
188
d4c93774 189SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 190 .id = "chronovu-la8",
b20eb520
UH
191 .name = "ChronoVu LA8",
192 .desc = "ChronoVu LA8 native file format data",
c7bc82ff 193 .exts = (const char*[]){"kdt", NULL},
02e24c0c
BV
194 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
195 .options = get_options,
83e9d586
UH
196 .format_match = format_match,
197 .init = init,
02e24c0c 198 .receive = receive,
7066fd46 199 .end = end,
ef9d3fef 200 .reset = reset,
83e9d586 201};