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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "input/binary"
31 #define MAX_CHUNK_SIZE 4096
32 #define DEFAULT_NUM_CHANNELS 8
33 #define DEFAULT_SAMPLERATE "0"
40 static int init(struct sr_input *in, GHashTable *options)
42 struct sr_channel *ch;
49 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
50 if (num_channels < 1) {
51 sr_err("Invalid value for numchannels: must be at least 1.");
55 s = g_variant_get_string(g_hash_table_lookup(options, "samplerate"), NULL);
56 if (sr_parse_sizestring(s, &samplerate) != SR_OK) {
57 sr_err("Invalid samplerate '%s'.", s);
61 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
62 in->priv = inc = g_malloc0(sizeof(struct context));
63 inc->samplerate = samplerate;
65 for (i = 0; i < num_channels; i++) {
66 snprintf(name, 16, "%d", i);
67 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
68 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
74 static int receive(const struct sr_input *in, GString *buf)
76 struct sr_datafeed_packet packet;
77 struct sr_datafeed_meta meta;
78 struct sr_datafeed_logic logic;
79 struct sr_config *src;
82 int chunk, num_channels;
86 g_string_append_len(in->buf, buf->str, buf->len);
88 num_channels = g_slist_length(in->sdi->channels);
90 std_session_send_df_header(in->sdi, LOG_PREFIX);
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);
100 packet.type = SR_DF_LOGIC;
101 packet.payload = &logic;
102 logic.unitsize = (num_channels + 7) / 8;
103 logic.data = in->buf->str;
105 /* Cut off at multiple of unitsize. */
106 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
109 for (i = 0; i < chunk_size; i += chunk) {
110 chunk = MAX(MAX_CHUNK_SIZE, chunk_size - i);
111 logic.length = chunk;
112 sr_session_send(in->sdi, &packet);
115 if (in->buf->len > chunk_size)
116 g_string_erase(in->buf, 0, in->buf->len - chunk_size);
121 static int cleanup(struct sr_input *in)
123 struct sr_datafeed_packet packet;
129 packet.type = SR_DF_END;
130 sr_session_send(in->sdi, &packet);
138 static struct sr_option options[] = {
139 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
140 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
144 static struct sr_option *get_options(void)
146 if (!options[0].def) {
147 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
148 options[1].def = g_variant_ref_sink(g_variant_new_string(DEFAULT_SAMPLERATE));
154 SR_PRIV struct sr_input_module input_binary = {
157 .desc = "Raw binary",
158 .options = get_options,