]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
Change sr_dev_inst_new() to take no parameters.
[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 30#define DEFAULT_NUM_CHANNELS 8
edd28877 31#define DEFAULT_SAMPLERATE 100000000L
02e24c0c
BV
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 55 struct context *inc;
ba7dd8bb 56 int num_channels, i;
02e24c0c
BV
57 char name[16];
58
59 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
60 if (num_channels < 1) {
61 sr_err("Invalid value for numchannels: must be at least 1.");
62 return SR_ERR_ARG;
63 }
64
0af636be 65 in->sdi = sr_dev_inst_new();
02e24c0c 66 in->priv = inc = g_malloc0(sizeof(struct context));
edd28877
BV
67
68 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 69
ba7dd8bb 70 for (i = 0; i < num_channels; i++) {
02e24c0c
BV
71 snprintf(name, 16, "%d", i);
72 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
ba7dd8bb 73 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
464d12c7 74 }
83e9d586
UH
75
76 return SR_OK;
77}
78
7066fd46 79static int process_buffer(struct sr_input *in)
83e9d586 80{
83e9d586 81 struct sr_datafeed_packet packet;
2df1e819 82 struct sr_datafeed_meta meta;
9c939c51 83 struct sr_datafeed_logic logic;
2df1e819 84 struct sr_config *src;
02e24c0c
BV
85 struct context *inc;
86 gsize chunk_size, i;
7066fd46 87 int chunk;
83e9d586 88
02e24c0c 89 inc = in->priv;
04c2f202
BV
90 if (!inc->started) {
91 std_session_send_df_header(in->sdi, LOG_PREFIX);
04c2f202 92
d0181813
BV
93 if (inc->samplerate) {
94 packet.type = SR_DF_META;
95 packet.payload = &meta;
96 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
97 meta.config = g_slist_append(NULL, src);
98 sr_session_send(in->sdi, &packet);
99 sr_config_free(src);
100 }
101
102 inc->started = TRUE;
04c2f202 103 }
83e9d586 104
83e9d586 105 packet.type = SR_DF_LOGIC;
9c939c51 106 packet.payload = &logic;
7066fd46 107 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
02e24c0c
BV
108
109 /* Cut off at multiple of unitsize. */
110 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
83e9d586 111
02e24c0c 112 for (i = 0; i < chunk_size; i += chunk) {
04c2f202
BV
113 logic.data = in->buf->str + i;
114 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
02e24c0c 115 logic.length = chunk;
5c3c1241 116 sr_session_send(in->sdi, &packet);
83e9d586 117 }
04c2f202 118 g_string_erase(in->buf, 0, chunk_size);
02e24c0c
BV
119
120 return SR_OK;
121}
122
7066fd46
BV
123static int receive(struct sr_input *in, GString *buf)
124{
125 int ret;
126
127 g_string_append_len(in->buf, buf->str, buf->len);
128
129 if (!in->sdi_ready) {
130 /* sdi is ready, notify frontend. */
131 in->sdi_ready = TRUE;
132 return SR_OK;
133 }
134
135 ret = process_buffer(in);
136
137 return ret;
138}
139
140static int end(struct sr_input *in)
02e24c0c 141{
02e24c0c 142 struct context *inc;
d0181813 143 struct sr_datafeed_packet packet;
7066fd46 144 int ret;
02e24c0c 145
7066fd46
BV
146 if (in->sdi_ready)
147 ret = process_buffer(in);
148 else
149 ret = SR_OK;
02e24c0c 150
7066fd46 151 inc = in->priv;
02e24c0c
BV
152 if (inc->started) {
153 packet.type = SR_DF_END;
154 sr_session_send(in->sdi, &packet);
155 }
83e9d586 156
7066fd46 157 return ret;
83e9d586
UH
158}
159
02e24c0c
BV
160static struct sr_option options[] = {
161 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
162 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
06ad20be 163 ALL_ZERO
02e24c0c
BV
164};
165
166static struct sr_option *get_options(void)
167{
168 if (!options[0].def) {
169 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
edd28877 170 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
02e24c0c
BV
171 }
172
173 return options;
174}
175
d4c93774 176SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 177 .id = "chronovu-la8",
02e24c0c 178 .name = "Chronovu-LA8",
17bfaca6 179 .desc = "ChronoVu LA8",
02e24c0c
BV
180 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
181 .options = get_options,
83e9d586
UH
182 .format_match = format_match,
183 .init = init,
02e24c0c 184 .receive = receive,
7066fd46 185 .end = end,
83e9d586 186};