2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "input/binary"
31 #define CHUNKSIZE (512 * 1024)
32 #define DEFAULT_NUM_CHANNELS 8
38 static int format_match(const char *filename)
42 /* This module will handle anything you throw at it. */
46 static int init(struct sr_input *in, const char *filename)
48 struct sr_channel *ch;
50 char name[SR_MAX_CHANNELNAME_LEN + 1];
56 if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
57 sr_err("Input format context malloc failed.");
61 num_channels = DEFAULT_NUM_CHANNELS;
65 param = g_hash_table_lookup(in->param, "numchannels");
67 num_channels = strtoul(param, NULL, 10);
72 param = g_hash_table_lookup(in->param, "samplerate");
74 if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK)
79 /* Create a virtual device. */
80 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
83 for (i = 0; i < num_channels; i++) {
84 snprintf(name, SR_MAX_CHANNELNAME_LEN, "%d", i);
85 /* TODO: Check return value. */
86 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name)))
88 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
94 static int loadfile(struct sr_input *in, const char *filename)
96 struct sr_datafeed_packet packet;
97 struct sr_datafeed_meta meta;
98 struct sr_datafeed_logic logic;
99 struct sr_config *src;
100 unsigned char buffer[CHUNKSIZE];
101 int fd, size, num_channels;
106 if ((fd = open(filename, O_RDONLY)) == -1)
109 num_channels = g_slist_length(in->sdi->channels);
111 /* Send header packet to the session bus. */
112 std_session_send_df_header(in->sdi, LOG_PREFIX);
114 if (ctx->samplerate) {
115 packet.type = SR_DF_META;
116 packet.payload = &meta;
117 src = sr_config_new(SR_CONF_SAMPLERATE,
118 g_variant_new_uint64(ctx->samplerate));
119 meta.config = g_slist_append(NULL, src);
120 sr_session_send(in->sdi, &packet);
124 /* Chop up the input file into chunks & send it to the session bus. */
125 packet.type = SR_DF_LOGIC;
126 packet.payload = &logic;
127 logic.unitsize = (num_channels + 7) / 8;
129 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
131 sr_session_send(in->sdi, &packet);
135 /* Send end packet to the session bus. */
136 packet.type = SR_DF_END;
137 sr_session_send(in->sdi, &packet);
145 SR_PRIV struct sr_input_format input_binary = {
147 .description = "Raw binary",
148 .format_match = format_match,
150 .loadfile = loadfile,