]> sigrok.org Git - libsigrok.git/blame - src/input/binary.c
input/binary: Increase chunk size from 4KB to 4MB.
[libsigrok.git] / src / input / binary.c
CommitLineData
34e4813f 1/*
50985c20 2 * This file is part of the libsigrok project.
34e4813f 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
34e4813f
BV
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
13a12913 21#include <stdlib.h>
34e4813f
BV
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <sys/time.h>
c1aae900 27#include <libsigrok/libsigrok.h>
45c59c8b 28#include "libsigrok-internal.h"
34e4813f 29
3544f848 30#define LOG_PREFIX "input/binary"
8e7f1cfd 31
a33e4be8 32#define MAX_CHUNK_SIZE (4 * 1024 * 1024)
d9251a2c
UH
33#define DEFAULT_NUM_CHANNELS 8
34#define DEFAULT_SAMPLERATE 0
13a12913 35
c506a6a6 36struct context {
b84cba4d 37 gboolean started;
0d1297a2 38 uint64_t samplerate;
408b6ab4 39 uint16_t unitsize;
c506a6a6
40};
41
b84cba4d 42static int init(struct sr_input *in, GHashTable *options)
13a12913 43{
b84cba4d 44 struct context *inc;
ba7dd8bb 45 int num_channels, i;
b84cba4d 46 char name[16];
543d45c5 47
b84cba4d
BV
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.");
51 return SR_ERR_ARG;
c506a6a6 52 }
13a12913 53
aac29cc1 54 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
b84cba4d 55 in->priv = inc = g_malloc0(sizeof(struct context));
10288172
BV
56
57 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 58
ba7dd8bb 59 for (i = 0; i < num_channels; i++) {
00ed77f2 60 snprintf(name, sizeof(name), "%d", i);
5e23fcab 61 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
464d12c7 62 }
13a12913 63
408b6ab4
UH
64 inc->unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
65
e46b8fb1 66 return SR_OK;
13a12913
BV
67}
68
7066fd46 69static int process_buffer(struct sr_input *in)
34e4813f 70{
b9c735a2 71 struct sr_datafeed_packet packet;
2df1e819 72 struct sr_datafeed_meta meta;
9c939c51 73 struct sr_datafeed_logic logic;
2df1e819 74 struct sr_config *src;
b84cba4d
BV
75 struct context *inc;
76 gsize chunk_size, i;
7066fd46 77 int chunk;
c506a6a6 78
b84cba4d 79 inc = in->priv;
88189019 80 if (!inc->started) {
bee2b016 81 std_session_send_df_header(in->sdi);
88189019 82
d0181813
BV
83 if (inc->samplerate) {
84 packet.type = SR_DF_META;
85 packet.payload = &meta;
86 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
87 meta.config = g_slist_append(NULL, src);
88 sr_session_send(in->sdi, &packet);
c01378c9 89 g_slist_free(meta.config);
d0181813
BV
90 sr_config_free(src);
91 }
92
93 inc->started = TRUE;
88189019 94 }
f366e86c 95
5a2326a7 96 packet.type = SR_DF_LOGIC;
9c939c51 97 packet.payload = &logic;
408b6ab4 98 logic.unitsize = inc->unitsize;
b84cba4d
BV
99
100 /* Cut off at multiple of unitsize. */
101 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
102
b84cba4d 103 for (i = 0; i < chunk_size; i += chunk) {
88189019
BV
104 logic.data = in->buf->str + i;
105 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
b84cba4d 106 logic.length = chunk;
5c3c1241 107 sr_session_send(in->sdi, &packet);
34e4813f 108 }
88189019 109 g_string_erase(in->buf, 0, chunk_size);
b84cba4d
BV
110
111 return SR_OK;
112}
113
7066fd46
BV
114static int receive(struct sr_input *in, GString *buf)
115{
116 int ret;
117
118 g_string_append_len(in->buf, buf->str, buf->len);
119
120 if (!in->sdi_ready) {
121 /* sdi is ready, notify frontend. */
122 in->sdi_ready = TRUE;
123 return SR_OK;
124 }
125
126 ret = process_buffer(in);
127
128 return ret;
129}
130
131static int end(struct sr_input *in)
b84cba4d 132{
b84cba4d 133 struct context *inc;
7066fd46 134 int ret;
34e4813f 135
7066fd46
BV
136 if (in->sdi_ready)
137 ret = process_buffer(in);
138 else
139 ret = SR_OK;
b84cba4d 140
7066fd46 141 inc = in->priv;
3be42bc2 142 if (inc->started)
bee2b016 143 std_session_send_df_end(in->sdi);
c506a6a6 144
7066fd46 145 return ret;
34e4813f
BV
146}
147
3781b65d
SA
148static int reset(struct sr_input *in)
149{
150 struct context *inc = in->priv;
151
152 inc->started = FALSE;
153 g_string_truncate(in->buf, 0);
154
155 return SR_OK;
156}
157
b84cba4d 158static struct sr_option options[] = {
867293a1
UH
159 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
160 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
06ad20be 161 ALL_ZERO
b84cba4d
BV
162};
163
2c240774 164static const struct sr_option *get_options(void)
b84cba4d
BV
165{
166 if (!options[0].def) {
167 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
10288172 168 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
b84cba4d
BV
169 }
170
171 return options;
172}
173
d4c93774 174SR_PRIV struct sr_input_module input_binary = {
cdb3573c 175 .id = "binary",
b84cba4d 176 .name = "Binary",
17bfaca6 177 .desc = "Raw binary",
c7bc82ff 178 .exts = NULL,
b84cba4d 179 .options = get_options,
d494a4aa 180 .init = init,
b84cba4d 181 .receive = receive,
7066fd46 182 .end = end,
3781b65d 183 .reset = reset,
34e4813f 184};