2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "input/binary"
32 #define CHUNK_SIZE (4 * 1024 * 1024)
33 #define DEFAULT_NUM_CHANNELS 8
34 #define DEFAULT_SAMPLERATE 0
42 static int init(struct sr_input *in, GHashTable *options)
48 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
49 if (num_channels < 1) {
50 sr_err("Invalid value for numchannels: must be at least 1.");
54 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
55 in->priv = inc = g_malloc0(sizeof(struct context));
57 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
59 for (i = 0; i < num_channels; i++) {
60 snprintf(name, sizeof(name), "%d", i);
61 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
64 inc->unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
69 static int process_buffer(struct sr_input *in)
71 struct sr_datafeed_packet packet;
72 struct sr_datafeed_logic logic;
79 std_session_send_df_header(in->sdi);
81 if (inc->samplerate) {
82 (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
83 g_variant_new_uint64(inc->samplerate));
89 packet.type = SR_DF_LOGIC;
90 packet.payload = &logic;
91 logic.unitsize = inc->unitsize;
93 /* Cut off at multiple of unitsize. */
94 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
96 for (i = 0; i < chunk_size; i += chunk) {
97 logic.data = in->buf->str + i;
98 chunk = MIN(CHUNK_SIZE, chunk_size - i);
99 chunk /= logic.unitsize;
100 chunk *= logic.unitsize;
101 logic.length = chunk;
102 sr_session_send(in->sdi, &packet);
104 g_string_erase(in->buf, 0, chunk_size);
109 static int receive(struct sr_input *in, GString *buf)
113 g_string_append_len(in->buf, buf->str, buf->len);
115 if (!in->sdi_ready) {
116 /* sdi is ready, notify frontend. */
117 in->sdi_ready = TRUE;
121 ret = process_buffer(in);
126 static int end(struct sr_input *in)
132 ret = process_buffer(in);
138 std_session_send_df_end(in->sdi);
143 static int reset(struct sr_input *in)
145 struct context *inc = in->priv;
147 inc->started = FALSE;
148 g_string_truncate(in->buf, 0);
153 static struct sr_option options[] = {
154 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
155 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
159 static const struct sr_option *get_options(void)
161 if (!options[0].def) {
162 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
163 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
169 SR_PRIV struct sr_input_module input_binary = {
172 .desc = "Raw binary logic data",
174 .options = get_options,