]> sigrok.org Git - libsigrok.git/blame - src/input/binary.c
input: Free instance-private storage at instance free.
[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
10288172 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 43 struct context *inc;
ba7dd8bb 44 int num_channels, i;
b84cba4d 45 char name[16];
543d45c5 46
b84cba4d
BV
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.");
50 return SR_ERR_ARG;
c506a6a6 51 }
13a12913 52
f2209364 53 in->sdi = sr_dev_inst_new(SR_ST_ACTIVE, NULL, NULL, NULL);
b84cba4d 54 in->priv = inc = g_malloc0(sizeof(struct context));
10288172
BV
55
56 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 57
ba7dd8bb 58 for (i = 0; i < num_channels; i++) {
b84cba4d
BV
59 snprintf(name, 16, "%d", i);
60 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name);
ba7dd8bb 61 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
464d12c7 62 }
13a12913 63
e46b8fb1 64 return SR_OK;
13a12913
BV
65}
66
d0181813 67static int receive(struct sr_input *in, GString *buf)
34e4813f 68{
b9c735a2 69 struct sr_datafeed_packet packet;
2df1e819 70 struct sr_datafeed_meta meta;
9c939c51 71 struct sr_datafeed_logic logic;
2df1e819 72 struct sr_config *src;
b84cba4d
BV
73 struct context *inc;
74 gsize chunk_size, i;
75 int chunk, num_channels;
c506a6a6 76
b84cba4d 77 inc = in->priv;
34e4813f 78
b84cba4d 79 g_string_append_len(in->buf, buf->str, buf->len);
34e4813f 80
ba7dd8bb 81 num_channels = g_slist_length(in->sdi->channels);
db91a1c3 82
d0181813
BV
83 if (!in->sdi_ready) {
84 /* sdi is ready, notify frontend. */
85 in->sdi_ready = TRUE;
86 return SR_OK;
87 }
88
88189019
BV
89 if (!inc->started) {
90 std_session_send_df_header(in->sdi, LOG_PREFIX);
88189019 91
d0181813
BV
92 if (inc->samplerate) {
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);
99 }
100
101 inc->started = TRUE;
88189019 102 }
f366e86c 103
5a2326a7 104 packet.type = SR_DF_LOGIC;
9c939c51 105 packet.payload = &logic;
ba7dd8bb 106 logic.unitsize = (num_channels + 7) / 8;
b84cba4d
BV
107
108 /* Cut off at multiple of unitsize. */
109 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
110
111 chunk = 0;
112 for (i = 0; i < chunk_size; i += chunk) {
88189019
BV
113 logic.data = in->buf->str + i;
114 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
b84cba4d 115 logic.length = chunk;
5c3c1241 116 sr_session_send(in->sdi, &packet);
34e4813f 117 }
88189019 118 g_string_erase(in->buf, 0, chunk_size);
b84cba4d
BV
119
120 return SR_OK;
121}
122
123static int cleanup(struct sr_input *in)
124{
b84cba4d 125 struct context *inc;
d0181813 126 struct sr_datafeed_packet packet;
34e4813f 127
b84cba4d 128 inc = in->priv;
d0181813
BV
129 if (!inc)
130 return SR_OK;
b84cba4d
BV
131
132 if (inc->started) {
133 packet.type = SR_DF_END;
134 sr_session_send(in->sdi, &packet);
135 }
c506a6a6 136
e46b8fb1 137 return SR_OK;
34e4813f
BV
138}
139
b84cba4d
BV
140static struct sr_option options[] = {
141 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
142 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
06ad20be 143 ALL_ZERO
b84cba4d
BV
144};
145
146static struct sr_option *get_options(void)
147{
148 if (!options[0].def) {
149 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
10288172 150 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
b84cba4d
BV
151 }
152
153 return options;
154}
155
d4c93774 156SR_PRIV struct sr_input_module input_binary = {
cdb3573c 157 .id = "binary",
b84cba4d 158 .name = "Binary",
17bfaca6 159 .desc = "Raw binary",
b84cba4d 160 .options = get_options,
d494a4aa 161 .init = init,
b84cba4d
BV
162 .receive = receive,
163 .cleanup = cleanup,
34e4813f 164};