]> sigrok.org Git - libsigrok.git/blob - src/input/binary.c
Build: Include <config.h> first in all source files
[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 MAX_CHUNK_SIZE        4096
33 #define DEFAULT_NUM_CHANNELS  8
34 #define DEFAULT_SAMPLERATE    0
35
36 struct context {
37         gboolean started;
38         uint64_t samplerate;
39 };
40
41 static int init(struct sr_input *in, GHashTable *options)
42 {
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 = g_malloc0(sizeof(struct sr_dev_inst));
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                 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
61         }
62
63         return SR_OK;
64 }
65
66 static int process_buffer(struct sr_input *in)
67 {
68         struct sr_datafeed_packet packet;
69         struct sr_datafeed_meta meta;
70         struct sr_datafeed_logic logic;
71         struct sr_config *src;
72         struct context *inc;
73         gsize chunk_size, i;
74         int chunk;
75
76         inc = in->priv;
77         if (!inc->started) {
78                 std_session_send_df_header(in->sdi, LOG_PREFIX);
79
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);
86                         sr_config_free(src);
87                 }
88
89                 inc->started = TRUE;
90         }
91
92         packet.type = SR_DF_LOGIC;
93         packet.payload = &logic;
94         logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
95
96         /* Cut off at multiple of unitsize. */
97         chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
98
99         for (i = 0; i < chunk_size; i += chunk) {
100                 logic.data = in->buf->str + i;
101                 chunk = MIN(MAX_CHUNK_SIZE, chunk_size - i);
102                 logic.length = chunk;
103                 sr_session_send(in->sdi, &packet);
104         }
105         g_string_erase(in->buf, 0, chunk_size);
106
107         return SR_OK;
108 }
109
110 static int receive(struct sr_input *in, GString *buf)
111 {
112         int ret;
113
114         g_string_append_len(in->buf, buf->str, buf->len);
115
116         if (!in->sdi_ready) {
117                 /* sdi is ready, notify frontend. */
118                 in->sdi_ready = TRUE;
119                 return SR_OK;
120         }
121
122         ret = process_buffer(in);
123
124         return ret;
125 }
126
127 static int end(struct sr_input *in)
128 {
129         struct context *inc;
130         struct sr_datafeed_packet packet;
131         int ret;
132
133         if (in->sdi_ready)
134                 ret = process_buffer(in);
135         else
136                 ret = SR_OK;
137
138         inc = in->priv;
139         if (inc->started) {
140                 packet.type = SR_DF_END;
141                 sr_session_send(in->sdi, &packet);
142         }
143
144         return ret;
145 }
146
147 static struct sr_option options[] = {
148         { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
149         { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
150         ALL_ZERO
151 };
152
153 static struct sr_option *get_options(void)
154 {
155         if (!options[0].def) {
156                 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
157                 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
158         }
159
160         return options;
161 }
162
163 SR_PRIV struct sr_input_module input_binary = {
164         .id = "binary",
165         .name = "Binary",
166         .desc = "Raw binary",
167         .exts = NULL,
168         .options = get_options,
169         .init = init,
170         .receive = receive,
171         .end = end,
172 };