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