]> sigrok.org Git - libsigrok.git/blame_incremental - src/input/chronovu_la8.c
Build: Include <config.h> first in all source files
[libsigrok.git] / src / input / chronovu_la8.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
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 <config.h>
22#include <stdlib.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <sys/stat.h>
26#include <libsigrok/libsigrok.h>
27#include "libsigrok-internal.h"
28
29#define LOG_PREFIX "input/chronovu-la8"
30
31#define DEFAULT_NUM_CHANNELS 8
32#define DEFAULT_SAMPLERATE SR_MHZ(100)
33#define MAX_CHUNK_SIZE (4 * 1024)
34#define CHRONOVU_LA8_FILESIZE ((8 * 1024 * 1024) + 5)
35
36struct context {
37 gboolean started;
38 uint64_t samplerate;
39};
40
41static int format_match(GHashTable *metadata)
42{
43 int size;
44
45 size = GPOINTER_TO_INT(g_hash_table_lookup(metadata,
46 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
47 if (size == CHRONOVU_LA8_FILESIZE)
48 return SR_OK;
49
50 return SR_ERR;
51}
52
53static int init(struct sr_input *in, GHashTable *options)
54{
55 struct context *inc;
56 int num_channels, i;
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
65 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
66 in->priv = inc = g_malloc0(sizeof(struct context));
67
68 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
69
70 for (i = 0; i < num_channels; i++) {
71 snprintf(name, 16, "%d", i);
72 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
73 }
74
75 return SR_OK;
76}
77
78static int process_buffer(struct sr_input *in)
79{
80 struct sr_datafeed_packet packet;
81 struct sr_datafeed_meta meta;
82 struct sr_datafeed_logic logic;
83 struct sr_config *src;
84 struct context *inc;
85 gsize chunk_size, i;
86 int chunk;
87
88 inc = in->priv;
89 if (!inc->started) {
90 std_session_send_df_header(in->sdi, LOG_PREFIX);
91
92 if (inc->samplerate) {
93 packet.type = SR_DF_META;
94 packet.payload = &meta;
95 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
96 meta.config = g_slist_append(NULL, src);
97 sr_session_send(in->sdi, &packet);
98 sr_config_free(src);
99 }
100
101 inc->started = TRUE;
102 }
103
104 packet.type = SR_DF_LOGIC;
105 packet.payload = &logic;
106 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
107
108 /* Cut off at multiple of unitsize. */
109 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
110
111 for (i = 0; i < chunk_size; i += chunk) {
112 logic.data = in->buf->str + i;
113 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
114 logic.length = chunk;
115 sr_session_send(in->sdi, &packet);
116 }
117 g_string_erase(in->buf, 0, chunk_size);
118
119 return SR_OK;
120}
121
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)
140{
141 struct context *inc;
142 struct sr_datafeed_packet packet;
143 int ret;
144
145 if (in->sdi_ready)
146 ret = process_buffer(in);
147 else
148 ret = SR_OK;
149
150 inc = in->priv;
151 if (inc->started) {
152 packet.type = SR_DF_END;
153 sr_session_send(in->sdi, &packet);
154 }
155
156 return ret;
157}
158
159static struct sr_option options[] = {
160 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
161 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
162 ALL_ZERO
163};
164
165static struct sr_option *get_options(void)
166{
167 if (!options[0].def) {
168 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
169 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
170 }
171
172 return options;
173}
174
175SR_PRIV struct sr_input_module input_chronovu_la8 = {
176 .id = "chronovu-la8",
177 .name = "Chronovu-LA8",
178 .desc = "ChronoVu LA8",
179 .exts = (const char*[]){"kdt", NULL},
180 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
181 .options = get_options,
182 .format_match = format_match,
183 .init = init,
184 .receive = receive,
185 .end = end,
186};