]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
input modules: Name chunk size #defines CHUNK_SIZE consistently.
[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)
8bc2fa6d 32#define CHUNK_SIZE (4 * 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
02e24c0c 40static int format_match(GHashTable *metadata)
83e9d586 41{
02e24c0c 42 int size;
83e9d586 43
02e24c0c
BV
44 size = GPOINTER_TO_INT(g_hash_table_lookup(metadata,
45 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
46 if (size == CHRONOVU_LA8_FILESIZE)
4f979115 47 return SR_OK;
83e9d586 48
4f979115 49 return SR_ERR;
83e9d586
UH
50}
51
02e24c0c 52static int init(struct sr_input *in, GHashTable *options)
83e9d586 53{
02e24c0c 54 struct context *inc;
ba7dd8bb 55 int num_channels, i;
02e24c0c
BV
56 char name[16];
57
58 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
59 if (num_channels < 1) {
60 sr_err("Invalid value for numchannels: must be at least 1.");
61 return SR_ERR_ARG;
62 }
63
aac29cc1 64 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
02e24c0c 65 in->priv = inc = g_malloc0(sizeof(struct context));
edd28877
BV
66
67 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 68
ba7dd8bb 69 for (i = 0; i < num_channels; i++) {
00ed77f2 70 snprintf(name, sizeof(name), "%d", i);
5e23fcab 71 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
464d12c7 72 }
83e9d586
UH
73
74 return SR_OK;
75}
76
7066fd46 77static int process_buffer(struct sr_input *in)
83e9d586 78{
83e9d586 79 struct sr_datafeed_packet packet;
2df1e819 80 struct sr_datafeed_meta meta;
9c939c51 81 struct sr_datafeed_logic logic;
2df1e819 82 struct sr_config *src;
02e24c0c
BV
83 struct context *inc;
84 gsize chunk_size, i;
7066fd46 85 int chunk;
83e9d586 86
02e24c0c 87 inc = in->priv;
04c2f202 88 if (!inc->started) {
bee2b016 89 std_session_send_df_header(in->sdi);
04c2f202 90
d0181813
BV
91 if (inc->samplerate) {
92 packet.type = SR_DF_META;
93 packet.payload = &meta;
94 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
95 meta.config = g_slist_append(NULL, src);
96 sr_session_send(in->sdi, &packet);
c01378c9 97 g_slist_free(meta.config);
d0181813
BV
98 sr_config_free(src);
99 }
100
101 inc->started = TRUE;
04c2f202 102 }
83e9d586 103
83e9d586 104 packet.type = SR_DF_LOGIC;
9c939c51 105 packet.payload = &logic;
7066fd46 106 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
02e24c0c
BV
107
108 /* Cut off at multiple of unitsize. */
109 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
83e9d586 110
02e24c0c 111 for (i = 0; i < chunk_size; i += chunk) {
04c2f202 112 logic.data = in->buf->str + i;
8bc2fa6d 113 chunk = MIN(CHUNK_SIZE, chunk_size - i);
02e24c0c 114 logic.length = chunk;
5c3c1241 115 sr_session_send(in->sdi, &packet);
83e9d586 116 }
04c2f202 117 g_string_erase(in->buf, 0, chunk_size);
02e24c0c
BV
118
119 return SR_OK;
120}
121
7066fd46
BV
122static int receive(struct sr_input *in, GString *buf)
123{
124 int ret;
125
126 g_string_append_len(in->buf, buf->str, buf->len);
127
128 if (!in->sdi_ready) {
129 /* sdi is ready, notify frontend. */
130 in->sdi_ready = TRUE;
131 return SR_OK;
132 }
133
134 ret = process_buffer(in);
135
136 return ret;
137}
138
139static int end(struct sr_input *in)
02e24c0c 140{
02e24c0c 141 struct context *inc;
7066fd46 142 int ret;
02e24c0c 143
7066fd46
BV
144 if (in->sdi_ready)
145 ret = process_buffer(in);
146 else
147 ret = SR_OK;
02e24c0c 148
7066fd46 149 inc = in->priv;
3be42bc2 150 if (inc->started)
bee2b016 151 std_session_send_df_end(in->sdi);
83e9d586 152
7066fd46 153 return ret;
83e9d586
UH
154}
155
ef9d3fef
SA
156static int reset(struct sr_input *in)
157{
158 struct context *inc = in->priv;
159
160 inc->started = FALSE;
161 g_string_truncate(in->buf, 0);
162
163 return SR_OK;
164}
165
02e24c0c 166static struct sr_option options[] = {
867293a1
UH
167 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
168 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
06ad20be 169 ALL_ZERO
02e24c0c
BV
170};
171
2c240774 172static const struct sr_option *get_options(void)
02e24c0c
BV
173{
174 if (!options[0].def) {
175 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
edd28877 176 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
02e24c0c
BV
177 }
178
179 return options;
180}
181
d4c93774 182SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 183 .id = "chronovu-la8",
02e24c0c 184 .name = "Chronovu-LA8",
17bfaca6 185 .desc = "ChronoVu LA8",
c7bc82ff 186 .exts = (const char*[]){"kdt", NULL},
02e24c0c
BV
187 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
188 .options = get_options,
83e9d586
UH
189 .format_match = format_match,
190 .init = init,
02e24c0c 191 .receive = receive,
7066fd46 192 .end = end,
ef9d3fef 193 .reset = reset,
83e9d586 194};