]> sigrok.org Git - libsigrok.git/blob - src/input/binary.c
51a64d099039c6ac859e3e6868f39c71e8ff0cbb
[libsigrok.git] / src / input / binary.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
20 #include <config.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <sys/time.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29
30 #define LOG_PREFIX "input/binary"
31
32 #define CHUNK_SIZE           (4 * 1024 * 1024)
33 #define DEFAULT_NUM_CHANNELS 8
34 #define DEFAULT_SAMPLERATE   0
35
36 struct context {
37         gboolean started;
38         uint64_t samplerate;
39         uint16_t unitsize;
40 };
41
42 static int init(struct sr_input *in, GHashTable *options)
43 {
44         struct context *inc;
45         int num_channels, i;
46         char name[16];
47
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;
52         }
53
54         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
55         in->priv = inc = g_malloc0(sizeof(struct context));
56
57         inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
58
59         for (i = 0; i < num_channels; i++) {
60                 snprintf(name, sizeof(name), "%d", i);
61                 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
62         }
63
64         inc->unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
65
66         return SR_OK;
67 }
68
69 static int process_buffer(struct sr_input *in)
70 {
71         struct sr_datafeed_packet packet;
72         struct sr_datafeed_meta meta;
73         struct sr_datafeed_logic logic;
74         struct sr_config *src;
75         struct context *inc;
76         gsize chunk_size, i;
77         int chunk;
78
79         inc = in->priv;
80         if (!inc->started) {
81                 std_session_send_df_header(in->sdi);
82
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);
89                         g_slist_free(meta.config);
90                         sr_config_free(src);
91                 }
92
93                 inc->started = TRUE;
94         }
95
96         packet.type = SR_DF_LOGIC;
97         packet.payload = &logic;
98         logic.unitsize = inc->unitsize;
99
100         /* Cut off at multiple of unitsize. */
101         chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
102
103         for (i = 0; i < chunk_size; i += chunk) {
104                 logic.data = in->buf->str + i;
105                 chunk = MIN(CHUNK_SIZE, chunk_size - i);
106                 chunk /= logic.unitsize;
107                 chunk *= logic.unitsize;
108                 logic.length = chunk;
109                 sr_session_send(in->sdi, &packet);
110         }
111         g_string_erase(in->buf, 0, chunk_size);
112
113         return SR_OK;
114 }
115
116 static int receive(struct sr_input *in, GString *buf)
117 {
118         int ret;
119
120         g_string_append_len(in->buf, buf->str, buf->len);
121
122         if (!in->sdi_ready) {
123                 /* sdi is ready, notify frontend. */
124                 in->sdi_ready = TRUE;
125                 return SR_OK;
126         }
127
128         ret = process_buffer(in);
129
130         return ret;
131 }
132
133 static int end(struct sr_input *in)
134 {
135         struct context *inc;
136         int ret;
137
138         if (in->sdi_ready)
139                 ret = process_buffer(in);
140         else
141                 ret = SR_OK;
142
143         inc = in->priv;
144         if (inc->started)
145                 std_session_send_df_end(in->sdi);
146
147         return ret;
148 }
149
150 static int reset(struct sr_input *in)
151 {
152         struct context *inc = in->priv;
153
154         inc->started = FALSE;
155         g_string_truncate(in->buf, 0);
156
157         return SR_OK;
158 }
159
160 static struct sr_option options[] = {
161         { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
162         { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
163         ALL_ZERO
164 };
165
166 static const struct sr_option *get_options(void)
167 {
168         if (!options[0].def) {
169                 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
170                 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
171         }
172
173         return options;
174 }
175
176 SR_PRIV struct sr_input_module input_binary = {
177         .id = "binary",
178         .name = "Binary",
179         .desc = "Raw binary logic data",
180         .exts = NULL,
181         .options = get_options,
182         .init = init,
183         .receive = receive,
184         .end = end,
185         .reset = reset,
186 };