]>
Commit | Line | Data |
---|---|---|
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 | |
d9251a2c UH |
32 | #define MAX_CHUNK_SIZE 4096 |
33 | #define DEFAULT_NUM_CHANNELS 8 | |
34 | #define DEFAULT_SAMPLERATE 0 | |
13a12913 | 35 | |
c506a6a6 | 36 | struct context { |
b84cba4d | 37 | gboolean started; |
0d1297a2 | 38 | uint64_t samplerate; |
c506a6a6 TŠ |
39 | }; |
40 | ||
b84cba4d | 41 | static int init(struct sr_input *in, GHashTable *options) |
13a12913 | 42 | { |
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 | |
aac29cc1 | 53 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
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 | 59 | snprintf(name, 16, "%d", i); |
5e23fcab | 60 | sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name); |
464d12c7 | 61 | } |
13a12913 | 62 | |
e46b8fb1 | 63 | return SR_OK; |
13a12913 BV |
64 | } |
65 | ||
7066fd46 | 66 | static int process_buffer(struct sr_input *in) |
34e4813f | 67 | { |
b9c735a2 | 68 | struct sr_datafeed_packet packet; |
2df1e819 | 69 | struct sr_datafeed_meta meta; |
9c939c51 | 70 | struct sr_datafeed_logic logic; |
2df1e819 | 71 | struct sr_config *src; |
b84cba4d BV |
72 | struct context *inc; |
73 | gsize chunk_size, i; | |
7066fd46 | 74 | int chunk; |
c506a6a6 | 75 | |
b84cba4d | 76 | inc = in->priv; |
88189019 | 77 | if (!inc->started) { |
bee2b016 | 78 | std_session_send_df_header(in->sdi); |
88189019 | 79 | |
d0181813 BV |
80 | if (inc->samplerate) { |
81 | packet.type = SR_DF_META; | |
82 | packet.payload = &meta; | |
83 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate)); | |
84 | meta.config = g_slist_append(NULL, src); | |
85 | sr_session_send(in->sdi, &packet); | |
c01378c9 | 86 | g_slist_free(meta.config); |
d0181813 BV |
87 | sr_config_free(src); |
88 | } | |
89 | ||
90 | inc->started = TRUE; | |
88189019 | 91 | } |
f366e86c | 92 | |
5a2326a7 | 93 | packet.type = SR_DF_LOGIC; |
9c939c51 | 94 | packet.payload = &logic; |
7066fd46 | 95 | logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8; |
b84cba4d BV |
96 | |
97 | /* Cut off at multiple of unitsize. */ | |
98 | chunk_size = in->buf->len / logic.unitsize * logic.unitsize; | |
99 | ||
b84cba4d | 100 | for (i = 0; i < chunk_size; i += chunk) { |
88189019 BV |
101 | logic.data = in->buf->str + i; |
102 | chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i); | |
b84cba4d | 103 | logic.length = chunk; |
5c3c1241 | 104 | sr_session_send(in->sdi, &packet); |
34e4813f | 105 | } |
88189019 | 106 | g_string_erase(in->buf, 0, chunk_size); |
b84cba4d BV |
107 | |
108 | return SR_OK; | |
109 | } | |
110 | ||
7066fd46 BV |
111 | static int receive(struct sr_input *in, GString *buf) |
112 | { | |
113 | int ret; | |
114 | ||
115 | g_string_append_len(in->buf, buf->str, buf->len); | |
116 | ||
117 | if (!in->sdi_ready) { | |
118 | /* sdi is ready, notify frontend. */ | |
119 | in->sdi_ready = TRUE; | |
120 | return SR_OK; | |
121 | } | |
122 | ||
123 | ret = process_buffer(in); | |
124 | ||
125 | return ret; | |
126 | } | |
127 | ||
128 | static int end(struct sr_input *in) | |
b84cba4d | 129 | { |
b84cba4d | 130 | struct context *inc; |
7066fd46 | 131 | int ret; |
34e4813f | 132 | |
7066fd46 BV |
133 | if (in->sdi_ready) |
134 | ret = process_buffer(in); | |
135 | else | |
136 | ret = SR_OK; | |
b84cba4d | 137 | |
7066fd46 | 138 | inc = in->priv; |
3be42bc2 | 139 | if (inc->started) |
bee2b016 | 140 | std_session_send_df_end(in->sdi); |
c506a6a6 | 141 | |
7066fd46 | 142 | return ret; |
34e4813f BV |
143 | } |
144 | ||
3781b65d SA |
145 | static int reset(struct sr_input *in) |
146 | { | |
147 | struct context *inc = in->priv; | |
148 | ||
149 | inc->started = FALSE; | |
150 | g_string_truncate(in->buf, 0); | |
151 | ||
152 | return SR_OK; | |
153 | } | |
154 | ||
b84cba4d BV |
155 | static struct sr_option options[] = { |
156 | { "numchannels", "Number of channels", "Number of channels", NULL, NULL }, | |
157 | { "samplerate", "Sample rate", "Sample rate", NULL, NULL }, | |
06ad20be | 158 | ALL_ZERO |
b84cba4d BV |
159 | }; |
160 | ||
2c240774 | 161 | static const struct sr_option *get_options(void) |
b84cba4d BV |
162 | { |
163 | if (!options[0].def) { | |
164 | options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS)); | |
10288172 | 165 | options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE)); |
b84cba4d BV |
166 | } |
167 | ||
168 | return options; | |
169 | } | |
170 | ||
d4c93774 | 171 | SR_PRIV struct sr_input_module input_binary = { |
cdb3573c | 172 | .id = "binary", |
b84cba4d | 173 | .name = "Binary", |
17bfaca6 | 174 | .desc = "Raw binary", |
c7bc82ff | 175 | .exts = NULL, |
b84cba4d | 176 | .options = get_options, |
d494a4aa | 177 | .init = init, |
b84cba4d | 178 | .receive = receive, |
7066fd46 | 179 | .end = end, |
3781b65d | 180 | .reset = reset, |
34e4813f | 181 | }; |