]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
input/binary: Use uint64 for samplerate option.
[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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
36423d04 24#include <sys/stat.h>
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
83e9d586 27
b95dd761 28#define LOG_PREFIX "input/chronovu-la8"
8e7f1cfd 29
02e24c0c
BV
30#define DEFAULT_NUM_CHANNELS 8
31#define DEFAULT_SAMPLERATE "100MHz"
32#define MAX_CHUNK_SIZE 4096
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{
ba7dd8bb 54 struct sr_channel *ch;
02e24c0c
BV
55 struct context *inc;
56 uint64_t samplerate;
ba7dd8bb 57 int num_channels, i;
02e24c0c
BV
58 const char *s;
59 char name[16];
60
61 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
62 if (num_channels < 1) {
63 sr_err("Invalid value for numchannels: must be at least 1.");
64 return SR_ERR_ARG;
65 }
66
67 s = g_variant_get_string(g_hash_table_lookup(options, "samplerate"), NULL);
68 if (sr_parse_sizestring(s, &samplerate) != SR_OK) {
69 sr_err("Invalid samplerate '%s'.", s);
70 return SR_ERR_ARG;
83e9d586
UH
71 }
72
5c3c1241 73 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
02e24c0c
BV
74 in->priv = inc = g_malloc0(sizeof(struct context));
75 inc->samplerate = samplerate;
464d12c7 76
ba7dd8bb 77 for (i = 0; i < num_channels; i++) {
02e24c0c
BV
78 snprintf(name, 16, "%d", i);
79 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
ba7dd8bb 80 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
464d12c7 81 }
83e9d586
UH
82
83 return SR_OK;
84}
85
02e24c0c 86static int receive(const struct sr_input *in, GString *buf)
83e9d586 87{
83e9d586 88 struct sr_datafeed_packet packet;
2df1e819 89 struct sr_datafeed_meta meta;
9c939c51 90 struct sr_datafeed_logic logic;
2df1e819 91 struct sr_config *src;
02e24c0c
BV
92 struct context *inc;
93 gsize chunk_size, i;
94 int chunk, num_channels;
83e9d586 95
02e24c0c 96 inc = in->priv;
83e9d586 97
02e24c0c 98 g_string_append_len(in->buf, buf->str, buf->len);
83e9d586 99
02e24c0c 100 num_channels = g_slist_length(in->sdi->channels);
83e9d586 101
29a27196 102 std_session_send_df_header(in->sdi, LOG_PREFIX);
02e24c0c 103 inc->started = TRUE;
f366e86c 104
2df1e819 105 packet.type = SR_DF_META;
f366e86c 106 packet.payload = &meta;
02e24c0c 107 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
2df1e819 108 meta.config = g_slist_append(NULL, src);
5c3c1241 109 sr_session_send(in->sdi, &packet);
ec4063b8 110 sr_config_free(src);
83e9d586 111
83e9d586 112 packet.type = SR_DF_LOGIC;
9c939c51 113 packet.payload = &logic;
ba7dd8bb 114 logic.unitsize = (num_channels + 7) / 8;
02e24c0c
BV
115 logic.data = in->buf->str;
116
117 /* Cut off at multiple of unitsize. */
118 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
83e9d586 119
02e24c0c
BV
120 chunk = 0;
121 for (i = 0; i < chunk_size; i += chunk) {
122 chunk = MAX(MAX_CHUNK_SIZE, chunk_size - i);
123 logic.length = chunk;
5c3c1241 124 sr_session_send(in->sdi, &packet);
83e9d586 125 }
83e9d586 126
02e24c0c
BV
127 if (in->buf->len > chunk_size)
128 g_string_erase(in->buf, 0, in->buf->len - chunk_size);
129
130 return SR_OK;
131}
132
133static int cleanup(struct sr_input *in)
134{
135 struct sr_datafeed_packet packet;
136 struct context *inc;
137
138 inc = in->priv;
139
140 if (inc->started) {
141 packet.type = SR_DF_END;
142 sr_session_send(in->sdi, &packet);
143 }
144 g_free(in->priv);
145 in->priv = NULL;
83e9d586
UH
146
147 return SR_OK;
148}
149
02e24c0c
BV
150static struct sr_option options[] = {
151 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
152 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
06ad20be 153 ALL_ZERO
02e24c0c
BV
154};
155
156static struct sr_option *get_options(void)
157{
158 if (!options[0].def) {
159 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
160 options[1].def = g_variant_ref_sink(g_variant_new_string(DEFAULT_SAMPLERATE));
161 }
162
163 return options;
164}
165
d4c93774 166SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 167 .id = "chronovu-la8",
02e24c0c 168 .name = "Chronovu-LA8",
17bfaca6 169 .desc = "ChronoVu LA8",
02e24c0c
BV
170 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
171 .options = get_options,
83e9d586
UH
172 .format_match = format_match,
173 .init = init,
02e24c0c
BV
174 .receive = receive,
175 .cleanup = cleanup,
83e9d586 176};