]> sigrok.org Git - libsigrok.git/blob - src/input/chronovu_la8.c
input: Add sdi_ready flag to struct sr_input.
[libsigrok.git] / src / input / chronovu_la8.c
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.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      100000000L
32 #define MAX_CHUNK_SIZE          4096
33 #define CHRONOVU_LA8_FILESIZE   8 * 1024 * 1024 + 5
34
35 struct context {
36         gboolean started;
37         uint64_t samplerate;
38 };
39
40 static 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
52 static int init(struct sr_input *in, GHashTable *options)
53 {
54         struct sr_channel *ch;
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 = sr_dev_inst_new(SR_ST_ACTIVE, NULL, NULL, NULL);
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                 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
73                 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
74         }
75
76         return SR_OK;
77 }
78
79 static int receive(struct sr_input *in, GString *buf)
80 {
81         struct sr_datafeed_packet packet;
82         struct sr_datafeed_meta meta;
83         struct sr_datafeed_logic logic;
84         struct sr_config *src;
85         struct context *inc;
86         gsize chunk_size, i;
87         int chunk, num_channels;
88
89         inc = in->priv;
90
91         g_string_append_len(in->buf, buf->str, buf->len);
92
93         num_channels = g_slist_length(in->sdi->channels);
94
95         if (!in->sdi_ready) {
96                 /* sdi is ready, notify frontend. */
97                 in->sdi_ready = TRUE;
98                 return SR_OK;
99         }
100
101         if (!inc->started) {
102                 std_session_send_df_header(in->sdi, LOG_PREFIX);
103
104                 if (inc->samplerate) {
105                         packet.type = SR_DF_META;
106                         packet.payload = &meta;
107                         src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
108                         meta.config = g_slist_append(NULL, src);
109                         sr_session_send(in->sdi, &packet);
110                         sr_config_free(src);
111                 }
112
113                 inc->started = TRUE;
114         }
115
116         packet.type = SR_DF_LOGIC;
117         packet.payload = &logic;
118         logic.unitsize = (num_channels + 7) / 8;
119
120         /* Cut off at multiple of unitsize. */
121         chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
122
123         chunk = 0;
124         for (i = 0; i < chunk_size; i += chunk) {
125                 logic.data = in->buf->str + i;
126                 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
127                 logic.length = chunk;
128                 sr_session_send(in->sdi, &packet);
129         }
130         g_string_erase(in->buf, 0, chunk_size);
131
132         return SR_OK;
133 }
134
135 static int cleanup(struct sr_input *in)
136 {
137         struct context *inc;
138         struct sr_datafeed_packet packet;
139
140         inc = in->priv;
141         if (!inc)
142                 return SR_OK;
143
144         if (inc->started) {
145                 packet.type = SR_DF_END;
146                 sr_session_send(in->sdi, &packet);
147         }
148         g_free(in->priv);
149         in->priv = NULL;
150
151         return SR_OK;
152 }
153
154 static struct sr_option options[] = {
155         { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
156         { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
157         ALL_ZERO
158 };
159
160 static struct sr_option *get_options(void)
161 {
162         if (!options[0].def) {
163                 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
164                 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
165         }
166
167         return options;
168 }
169
170 SR_PRIV struct sr_input_module input_chronovu_la8 = {
171         .id = "chronovu-la8",
172         .name = "Chronovu-LA8",
173         .desc = "ChronoVu LA8",
174         .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
175         .options = get_options,
176         .format_match = format_match,
177         .init = init,
178         .receive = receive,
179         .cleanup = cleanup,
180 };