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 MAX_CHUNK_SIZE 4096
33 #define DEFAULT_NUM_CHANNELS 8
34 #define DEFAULT_SAMPLERATE 0
41 static int init(struct sr_input *in, GHashTable *options)
47 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
48 if (num_channels < 1) {
49 sr_err("Invalid value for numchannels: must be at least 1.");
53 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
54 in->priv = inc = g_malloc0(sizeof(struct context));
56 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
58 for (i = 0; i < num_channels; i++) {
59 snprintf(name, 16, "%d", i);
60 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
66 static int process_buffer(struct sr_input *in)
68 struct sr_datafeed_packet packet;
69 struct sr_datafeed_meta meta;
70 struct sr_datafeed_logic logic;
71 struct sr_config *src;
78 std_session_send_df_header(in->sdi);
80 if (inc->samplerate) {
81 packet.type = SR_DF_META;
82 packet.payload = &meta;
83 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
84 meta.config = g_slist_append(NULL, src);
85 sr_session_send(in->sdi, &packet);
86 g_slist_free(meta.config);
93 packet.type = SR_DF_LOGIC;
94 packet.payload = &logic;
95 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
97 /* Cut off at multiple of unitsize. */
98 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
100 for (i = 0; i < chunk_size; i += chunk) {
101 logic.data = in->buf->str + i;
102 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
103 logic.length = chunk;
104 sr_session_send(in->sdi, &packet);
106 g_string_erase(in->buf, 0, chunk_size);
111 static int receive(struct sr_input *in, GString *buf)
115 g_string_append_len(in->buf, buf->str, buf->len);
117 if (!in->sdi_ready) {
118 /* sdi is ready, notify frontend. */
119 in->sdi_ready = TRUE;
123 ret = process_buffer(in);
128 static int end(struct sr_input *in)
134 ret = process_buffer(in);
140 std_session_send_df_end(in->sdi);
145 static int reset(struct sr_input *in)
147 struct context *inc = in->priv;
149 inc->started = FALSE;
150 g_string_truncate(in->buf, 0);
155 static struct sr_option options[] = {
156 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
157 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
161 static const struct sr_option *get_options(void)
163 if (!options[0].def) {
164 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
165 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
171 SR_PRIV struct sr_input_module input_binary = {
174 .desc = "Raw binary",
176 .options = get_options,