]> sigrok.org Git - libsigrok.git/blame - input/binary.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / input / binary.c
CommitLineData
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
8ff6afc9 31#define CHUNKSIZE (512 * 1024)
13a12913
BV
32#define DEFAULT_NUM_PROBES 8
33
c506a6a6 34struct context {
0d1297a2 35 uint64_t samplerate;
c506a6a6
36};
37
757b8c62 38static int format_match(const char *filename)
34e4813f 39{
719c5a93 40 (void)filename;
13a12913 41
8e7f1cfd 42 /* This module will handle anything you throw at it. */
34e4813f
BV
43 return TRUE;
44}
45
543d45c5 46static int init(struct sr_input *in, const char *filename)
13a12913 47{
ba7dd8bb
UH
48 struct sr_channel *ch;
49 int num_channels, i;
c37d2b1b 50 char name[SR_MAX_PROBENAME_LEN + 1];
c506a6a6
51 char *param;
52 struct context *ctx;
53
543d45c5
BV
54 (void)filename;
55
c506a6a6 56 if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
8e7f1cfd 57 sr_err("Input format context malloc failed.");
c506a6a6
58 return SR_ERR_MALLOC;
59 }
13a12913 60
ba7dd8bb 61 num_channels = DEFAULT_NUM_PROBES;
c506a6a6
62 ctx->samplerate = 0;
63
8e7f1cfd 64 if (in->param) {
ba7dd8bb 65 param = g_hash_table_lookup(in->param, "numchannels");
c506a6a6 66 if (param) {
ba7dd8bb
UH
67 num_channels = strtoul(param, NULL, 10);
68 if (num_channels < 1)
c506a6a6
69 return SR_ERR;
70 }
71
72 param = g_hash_table_lookup(in->param, "samplerate");
73 if (param) {
0d1297a2 74 if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK)
c506a6a6 75 return SR_ERR;
c506a6a6 76 }
c37d2b1b 77 }
13a12913 78
c37d2b1b 79 /* Create a virtual device. */
5c3c1241 80 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
c506a6a6 81 in->internal = ctx;
464d12c7 82
ba7dd8bb 83 for (i = 0; i < num_channels; i++) {
464d12c7 84 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
c37d2b1b 85 /* TODO: Check return value. */
ba7dd8bb 86 if (!(ch = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
5c3c1241 87 return SR_ERR;
ba7dd8bb 88 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
464d12c7 89 }
13a12913 90
e46b8fb1 91 return SR_OK;
13a12913
BV
92}
93
f50f3f40 94static int loadfile(struct sr_input *in, const char *filename)
34e4813f 95{
b9c735a2 96 struct sr_datafeed_packet packet;
2df1e819 97 struct sr_datafeed_meta meta;
9c939c51 98 struct sr_datafeed_logic logic;
2df1e819 99 struct sr_config *src;
9c939c51 100 unsigned char buffer[CHUNKSIZE];
ba7dd8bb 101 int fd, size, num_channels;
c506a6a6
102 struct context *ctx;
103
104 ctx = in->internal;
34e4813f 105
757b8c62 106 if ((fd = open(filename, O_RDONLY)) == -1)
e46b8fb1 107 return SR_ERR;
34e4813f 108
ba7dd8bb 109 num_channels = g_slist_length(in->sdi->channels);
db91a1c3 110
8e7f1cfd 111 /* Send header packet to the session bus. */
29a27196 112 std_session_send_df_header(in->sdi, LOG_PREFIX);
34e4813f 113
2df1e819
BV
114 if (ctx->samplerate) {
115 packet.type = SR_DF_META;
116 packet.payload = &meta;
ec4063b8
BV
117 src = sr_config_new(SR_CONF_SAMPLERATE,
118 g_variant_new_uint64(ctx->samplerate));
2df1e819
BV
119 meta.config = g_slist_append(NULL, src);
120 sr_session_send(in->sdi, &packet);
ec4063b8 121 sr_config_free(src);
2df1e819 122 }
f366e86c 123
8e7f1cfd 124 /* Chop up the input file into chunks & send it to the session bus. */
5a2326a7 125 packet.type = SR_DF_LOGIC;
9c939c51 126 packet.payload = &logic;
ba7dd8bb 127 logic.unitsize = (num_channels + 7) / 8;
9c939c51 128 logic.data = buffer;
757b8c62 129 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
9c939c51 130 logic.length = size;
5c3c1241 131 sr_session_send(in->sdi, &packet);
34e4813f
BV
132 }
133 close(fd);
134
8e7f1cfd 135 /* Send end packet to the session bus. */
5a2326a7 136 packet.type = SR_DF_END;
5c3c1241 137 sr_session_send(in->sdi, &packet);
34e4813f 138
c506a6a6
139 g_free(ctx);
140 in->internal = NULL;
141
e46b8fb1 142 return SR_OK;
34e4813f
BV
143}
144
7c1d391c 145SR_PRIV struct sr_input_format input_binary = {
cdb3573c 146 .id = "binary",
d494a4aa
UH
147 .description = "Raw binary",
148 .format_match = format_match,
149 .init = init,
150 .loadfile = loadfile,
34e4813f 151};