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