]>
Commit | Line | Data |
---|---|---|
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 <stdlib.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/time.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | ||
29 | #define LOG_PREFIX "input/binary" | |
30 | ||
31 | #define MAX_CHUNK_SIZE 4096 | |
32 | #define DEFAULT_NUM_CHANNELS 8 | |
33 | #define DEFAULT_SAMPLERATE 0 | |
34 | ||
35 | struct context { | |
36 | gboolean started; | |
37 | uint64_t samplerate; | |
38 | }; | |
39 | ||
40 | static int init(struct sr_input *in, GHashTable *options) | |
41 | { | |
42 | struct sr_channel *ch; | |
43 | struct context *inc; | |
44 | int num_channels, i; | |
45 | char name[16]; | |
46 | ||
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; | |
51 | } | |
52 | ||
53 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); | |
54 | in->priv = inc = g_malloc0(sizeof(struct context)); | |
55 | ||
56 | inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); | |
57 | ||
58 | for (i = 0; i < num_channels; i++) { | |
59 | snprintf(name, 16, "%d", i); | |
60 | ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name); | |
61 | in->sdi->channels = g_slist_append(in->sdi->channels, ch); | |
62 | } | |
63 | ||
64 | return SR_OK; | |
65 | } | |
66 | ||
67 | static int receive(const struct sr_input *in, GString *buf) | |
68 | { | |
69 | struct sr_datafeed_packet packet; | |
70 | struct sr_datafeed_meta meta; | |
71 | struct sr_datafeed_logic logic; | |
72 | struct sr_config *src; | |
73 | struct context *inc; | |
74 | gsize chunk_size, i; | |
75 | int chunk, num_channels; | |
76 | ||
77 | inc = in->priv; | |
78 | ||
79 | g_string_append_len(in->buf, buf->str, buf->len); | |
80 | ||
81 | num_channels = g_slist_length(in->sdi->channels); | |
82 | ||
83 | std_session_send_df_header(in->sdi, LOG_PREFIX); | |
84 | inc->started = TRUE; | |
85 | ||
86 | packet.type = SR_DF_META; | |
87 | packet.payload = &meta; | |
88 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate)); | |
89 | meta.config = g_slist_append(NULL, src); | |
90 | sr_session_send(in->sdi, &packet); | |
91 | sr_config_free(src); | |
92 | ||
93 | packet.type = SR_DF_LOGIC; | |
94 | packet.payload = &logic; | |
95 | logic.unitsize = (num_channels + 7) / 8; | |
96 | logic.data = in->buf->str; | |
97 | ||
98 | /* Cut off at multiple of unitsize. */ | |
99 | chunk_size = in->buf->len / logic.unitsize * logic.unitsize; | |
100 | ||
101 | chunk = 0; | |
102 | for (i = 0; i < chunk_size; i += chunk) { | |
103 | chunk = MAX(MAX_CHUNK_SIZE, chunk_size - i); | |
104 | logic.length = chunk; | |
105 | sr_session_send(in->sdi, &packet); | |
106 | } | |
107 | ||
108 | if (in->buf->len > chunk_size) | |
109 | g_string_erase(in->buf, 0, in->buf->len - chunk_size); | |
110 | ||
111 | return SR_OK; | |
112 | } | |
113 | ||
114 | static int cleanup(struct sr_input *in) | |
115 | { | |
116 | struct sr_datafeed_packet packet; | |
117 | struct context *inc; | |
118 | ||
119 | inc = in->priv; | |
120 | ||
121 | if (inc->started) { | |
122 | packet.type = SR_DF_END; | |
123 | sr_session_send(in->sdi, &packet); | |
124 | } | |
125 | g_free(in->priv); | |
126 | in->priv = NULL; | |
127 | ||
128 | return SR_OK; | |
129 | } | |
130 | ||
131 | static struct sr_option options[] = { | |
132 | { "numchannels", "Number of channels", "Number of channels", NULL, NULL }, | |
133 | { "samplerate", "Sample rate", "Sample rate", NULL, NULL }, | |
134 | ALL_ZERO | |
135 | }; | |
136 | ||
137 | static struct sr_option *get_options(void) | |
138 | { | |
139 | if (!options[0].def) { | |
140 | options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS)); | |
141 | options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE)); | |
142 | } | |
143 | ||
144 | return options; | |
145 | } | |
146 | ||
147 | SR_PRIV struct sr_input_module input_binary = { | |
148 | .id = "binary", | |
149 | .name = "Binary", | |
150 | .desc = "Raw binary", | |
151 | .options = get_options, | |
152 | .init = init, | |
153 | .receive = receive, | |
154 | .cleanup = cleanup, | |
155 | }; |