]> sigrok.org Git - libsigrok.git/blame - src/input/binary.c
input: Avoid warnings on all-zero static struct entries.
[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
13a12913 20#include <stdlib.h>
34e4813f
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <sys/time.h>
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
34e4813f 28
3544f848 29#define LOG_PREFIX "input/binary"
8e7f1cfd 30
b84cba4d 31#define MAX_CHUNK_SIZE 4096
3f239f08 32#define DEFAULT_NUM_CHANNELS 8
b84cba4d 33#define DEFAULT_SAMPLERATE "0"
13a12913 34
c506a6a6 35struct context {
b84cba4d 36 gboolean started;
0d1297a2 37 uint64_t samplerate;
c506a6a6
38};
39
b84cba4d 40static int init(struct sr_input *in, GHashTable *options)
13a12913 41{
ba7dd8bb 42 struct sr_channel *ch;
b84cba4d
BV
43 struct context *inc;
44 uint64_t samplerate;
ba7dd8bb 45 int num_channels, i;
b84cba4d
BV
46 const char *s;
47 char name[16];
543d45c5 48
b84cba4d
BV
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.");
52 return SR_ERR_ARG;
c506a6a6 53 }
13a12913 54
b84cba4d
BV
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);
58 return SR_ERR_ARG;
c37d2b1b 59 }
13a12913 60
5c3c1241 61 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
b84cba4d
BV
62 in->priv = inc = g_malloc0(sizeof(struct context));
63 inc->samplerate = samplerate;
464d12c7 64
ba7dd8bb 65 for (i = 0; i < num_channels; i++) {
b84cba4d
BV
66 snprintf(name, 16, "%d", i);
67 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
ba7dd8bb 68 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
464d12c7 69 }
13a12913 70
e46b8fb1 71 return SR_OK;
13a12913
BV
72}
73
b84cba4d 74static int receive(const struct sr_input *in, GString *buf)
34e4813f 75{
b9c735a2 76 struct sr_datafeed_packet packet;
2df1e819 77 struct sr_datafeed_meta meta;
9c939c51 78 struct sr_datafeed_logic logic;
2df1e819 79 struct sr_config *src;
b84cba4d
BV
80 struct context *inc;
81 gsize chunk_size, i;
82 int chunk, num_channels;
c506a6a6 83
b84cba4d 84 inc = in->priv;
34e4813f 85
b84cba4d 86 g_string_append_len(in->buf, buf->str, buf->len);
34e4813f 87
ba7dd8bb 88 num_channels = g_slist_length(in->sdi->channels);
db91a1c3 89
29a27196 90 std_session_send_df_header(in->sdi, LOG_PREFIX);
b84cba4d 91 inc->started = TRUE;
34e4813f 92
b84cba4d
BV
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);
98 sr_config_free(src);
f366e86c 99
5a2326a7 100 packet.type = SR_DF_LOGIC;
9c939c51 101 packet.payload = &logic;
ba7dd8bb 102 logic.unitsize = (num_channels + 7) / 8;
b84cba4d
BV
103 logic.data = in->buf->str;
104
105 /* Cut off at multiple of unitsize. */
106 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
107
108 chunk = 0;
109 for (i = 0; i < chunk_size; i += chunk) {
110 chunk = MAX(MAX_CHUNK_SIZE, chunk_size - i);
111 logic.length = chunk;
5c3c1241 112 sr_session_send(in->sdi, &packet);
34e4813f 113 }
34e4813f 114
b84cba4d
BV
115 if (in->buf->len > chunk_size)
116 g_string_erase(in->buf, 0, in->buf->len - chunk_size);
117
118 return SR_OK;
119}
120
121static int cleanup(struct sr_input *in)
122{
123 struct sr_datafeed_packet packet;
124 struct context *inc;
34e4813f 125
b84cba4d
BV
126 inc = in->priv;
127
128 if (inc->started) {
129 packet.type = SR_DF_END;
130 sr_session_send(in->sdi, &packet);
131 }
132 g_free(in->priv);
133 in->priv = NULL;
c506a6a6 134
e46b8fb1 135 return SR_OK;
34e4813f
BV
136}
137
b84cba4d
BV
138static struct sr_option options[] = {
139 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
140 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
06ad20be 141 ALL_ZERO
b84cba4d
BV
142};
143
144static struct sr_option *get_options(void)
145{
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));
149 }
150
151 return options;
152}
153
d4c93774 154SR_PRIV struct sr_input_module input_binary = {
cdb3573c 155 .id = "binary",
b84cba4d 156 .name = "Binary",
17bfaca6 157 .desc = "Raw binary",
b84cba4d 158 .options = get_options,
d494a4aa 159 .init = init,
b84cba4d
BV
160 .receive = receive,
161 .cleanup = cleanup,
34e4813f 162};